package com.hzz.web.app.examine;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hzz.api.examine.IExamineResultService;
import com.hzz.dto.examine.ApiExamineResultDto;
import com.hzz.dto.examine.ExamineResultDto;
import com.hzz.framework.Response;
import com.hzz.framework.util.DateUtil;
import com.hzz.framework.web.controller.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

@Api(tags = "考核结果")
@RestController
@Slf4j
public class ApiExamineResultController extends BaseController {
	@Autowired
	IExamineResultService examineResultService;
	static final String acPrefix = "/api/examine/examineResult/";

	@ApiOperation(value = "考核情况")
	@RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = acPrefix + "info")
	public Response<List<ApiExamineResultDto>> info(
			@RequestParam(required = false, name = "areaId") @ApiParam("行政区划最后一级ID") Long areaId,
			@RequestParam(required = true, name = "period") @ApiParam("考核季度(必填)：1上年度考核，2上季度考核，3上月度考核") Integer period) {
		log.info("info.........");
		Response result = new Response();
		try {
			ExamineResultDto dto = new ExamineResultDto();
			dto.setCurrAreaId(areaId);
			if (period.intValue() == 1) {// 上年度，获取上年度开始和结束日期
				Integer year = DateUtil.getLastYear();
				dto.setDateBegin(year + "-01-01");
				dto.setDateEnd(year + "-12-31");
			} else if (period.intValue() == 2) {// 上季度，获取上季度开始和结束日期
				dto.setDateBegin(DateUtil.getLastQuarterFirstDay());
				dto.setDateEnd(DateUtil.getLastQuarterLastDay());
			} else if (period.intValue() == 3) {// 上月度，获取上月度开始和结束日期
				dto.setDateBegin(DateUtil.getLastMonthFirstDay());
				dto.setDateEnd(DateUtil.getLastMonthLastDay());
			}
			result.data = examineResultService.findDataForApp(dto);
		} catch (Exception e) {
			result = Response.error(e.getMessage());
		}
		return result;
	}

	@ApiOperation(value = "下属考核")
	@RequestMapping(method = RequestMethod.GET, value = acPrefix + "underling")
	public Response<List<ApiExamineResultDto>> underling(
			@RequestParam(required = true, name = "userId") @ApiParam("河长ID(必填)") Long userId,
			@RequestParam(required = true, name = "period") @ApiParam("考核季度(必填)：1上年度考核，2上季度考核，3上月度考核") Integer period) {
		log.info("underling.........");
		Response result = new Response();
		try {
			ExamineResultDto dto = new ExamineResultDto();
			dto.setUserId(userId);
			if (period.intValue() == 1) {// 上年度，获取上年度开始和结束日期
				Integer year = DateUtil.getLastYear();
				dto.setDateBegin(year + "-01-01");
				dto.setDateEnd(year + "-12-31");
			} else if (period.intValue() == 2) {// 上季度，获取上季度开始和结束日期
				dto.setDateBegin(DateUtil.getLastQuarterFirstDay());
				dto.setDateEnd(DateUtil.getLastQuarterLastDay());
			} else if (period.intValue() == 3) {// 上月度，获取上月度开始和结束日期
				dto.setDateBegin(DateUtil.getLastMonthFirstDay());
				dto.setDateEnd(DateUtil.getLastMonthLastDay());
			}
			result.data = examineResultService.findDataForApp(dto);
		} catch (Exception e) {
			result = Response.error(e.getMessage());
		}
		return result;
	}
}